* ***************************************************************** *
* ***************************************************************** *
*   File-Name:      Partisan vs. impartial PCTs 		      	    *
*   Author:         Christoph Valentin Steinert					    *
*   Date:           5 August, 2019                                 	*
* ***************************************************************** *
* ***************************************************************** *

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* The Analysis was conducted with Stata/SE 15.1

* Install the following
*ssc install outreg2
*ssc install cem
*ssc install coefplot
*ssc install estout
*ssc install spmap
*ssc install shp2dta
*ssc install mif2dta

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
hist gdpcap, freq
*highly skewed distribution --> logarithmic transformation
gen gdpcap_log = log(gdpcap)
hist gdpcap_log, freq
hist gdppercapitappp, freq
gen gdpcappp_log = log(gdppercapitappp)
hist gdpcappp_log, freq
hist btldeath, freq
sum btldeath, detail
*highly skewed distribution --> logarithmic tansformation
gen battledeath_log = log(btldeath)
hist battledeath_log, freq

* Generate victory-dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate bargained-solution-dummy
gen bargainsol = .
replace bargainsol = 1 if termination == 2
replace bargainsol = 0 if termination == 1
replace bargainsol = 0 if termination == 3
replace bargainsol = 0 if termination == .

* Generate territorial-conflict-dummy:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Generate post-cold-war-dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Label Variables					
lab var peacekeep "Peacekeeping"				
lab define peacekeep ///
                0  "no Peacekeeping" ///
                1  "Peacekeeping" 
		
lab var prelapse "Post-conflict relapse until t5"
lab define prelapse ///
				0  "no post-conflict relapse" ///
				1  "post-conflict relapse"
				
lab var milexp "Military expenditure Ø until t+5"

lab var gdpcap "GDP per capita"

lab var publicstrengthtruth "Public strength of truth commission"
lab define publicstrengthtruth ///
				3  "public hearings & public report & naming of perpetrators" ///
				2  "two of them" ///
				1  "one of them" ///
				0  "none of them" 
				
lab var peacesurvival "Years of peace survival until post-conflict relapse, 100 if no relapse"

lab var conlapse "Conflict relapse until t5 with alternative coding criteria, just any conflict in country, names irrelevant"
lab define conlapse ///
				0  "no post-conflict relapse" ///
				1  "post-conflict relapse"
				
lab var consurvival "Years of peace survival until post-conflict relapse, alternative coding scheme, parties irrelevant"	

lab var battledeath_log "Logged number of battle deaths"	

lab var duration "Conflict duration"

lab var victory "Conflict termination with victory of one party"
lab define victory ///
				0  "bargained solution or other" ///
				1  "victory"	

lab var bargainsol "Conflict termination with bargained solution"
lab define bargainsol ///
				0  "victory or other" ///
				1  "bargained solution"	

lab var territorialconfdum "Territorial conflict-dummy"
lab define territorialconfdum ///
				0  "no territorial conflict" ///
				1  "territorial conflict"	
				
lab var gdpcap_log "GDP per capita logged"

lab var PTSatT "Value of Political Terror Scale at t"

lab var PTSatT5 "Value of Political Terror Scale at t+5"

lab var polityt "Level of democratisation with Polity at t"

lab var ethnicfrac "Ethnic Fractionalisation Index at t derived from Ethnic Power Relations Dataset"

* Multiple post-conflict justice mechanisms
gen onlytrial = .
replace onlytrial = 0
replace onlytrial = 1 if trial == 1 & pcj == 1
gen onlyamnesty = .
replace onlyamnesty = 0
replace onlyamnesty = 1 if amnesty == 1 & pcj == 1

list trial amnesty truth rep purge exile if pcj == 2 // to identify cases with two pcjs

* Combinations of amnesties and trials
gen amnestyANDtrial = .
replace amnestyANDtrial = 0
replace amnestyANDtrial = 1 if amnesty == 1 & trial == 1
replace amnestyANDtrial = . if trial == .

* Collapsing some categories
gen amnestyANDothers = .
replace amnestyANDothers = 0
replace amnestyANDothers = 1 if amnesty == 1 & rep == 1 & pcj == 2
replace amnestyANDothers = 1 if amnesty == 1 & exile == 1 & pcj == 2
replace amnestyANDothers = 1 if amnesty == 1 & purge == 1 & pcj == 2
replace amnestyANDothers = 1 if amnesty == 1 & rep == 1 & purge == 1 & pcj == 3

gen trialANDothers = .
replace trialANDothers = 0
replace trialANDothers = 1 if trial == 1 & rep == 1 & pcj == 2
replace trialANDothers = 1 if trial == 1 & exile == 1 & pcj == 2
replace trialANDothers = 1 if trial == 1 & purge == 1 & pcj == 2
replace trialANDothers = 1 if trial == 1 & purge == 1 & exile == 1 & pcj == 3
replace trialANDothers = 1 if trial == 1 & rep == 1 & exile == 1 & pcj == 3
replace trialANDothers = 1 if trial == 1 & purge == 1 & rep == 1 & pcj == 3

* Trial scope issue
codebook trial_scope
* 1 (individuals) = 20
* 2 (elites) = 16
* 3 (specific group or subset of group) = 42
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > peacesurvival

* Generate mean-ratings of survey items
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Generate fair trial & unfair trial based on Q7
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0
tab impartial
tab partisan

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
factor Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
greigen 
loadingplot
rotate, promax
loadingplot
factor Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
loadingplot
predict f1 
* Higher values more impartial, lower values more partisan
alpha Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
* Cronbach alphas should be at least 0.7, here 0.81 

* Generate impactial trial & partisan trial with index
sum f1, detail
gen indexvalue = 0
replace indexvalue = f1 if f1 != .
* To find a midpoint of the index that is not distorted by the larger number of cases of post-conflict injustice, I checked its maximum value for a hyptothetical case with only tens on each question and its minimum value
* for a hypothetical case with only zeros. Those values are 2.681 and -1.744712 respectively. That means, the midpoint is 0.468144.
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.468144
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.468144
replace impartialtrial = . if f1 == .
replace impartialtrial= 0 if trialext == 0
tab impartialtrial

* Test conformity between different trial measures
corr partisan unfairtrial partisantrial
list partisan unfairtrial partisantrial
tab pperid if partisan == 1 & unfairtrial == 1 & partisantrial == 1

* Time between PCT implementation and trial-year
gen timelapse = .
replace timelapse = trial_year - epend
sum timelapse if unfairtrial == 1
sum timelapse if fairtrial == 1

* Level of autocratization in countries with fair trial & unfair trial
sum polityt if unfairtrial == 1
sum polityt if fairtrial == 1
gen ttestvar = 1 if fairtrial == 1
replace ttestvar = 2 if unfairtrial == 1
* Two-sample t-test
ttest polityt, by(ttestvar)
sum polityt5 if unfairtrial == 1
sum polityt5 if fairtrial == 1
gen polityimprov = polityt5 - polityt
sum polityimprov if unfairtrial == 1
sum polityimprov if fairtrial == 1

* Level of autocratization in countries with fair trial & unfair trial/ alternative coding
sum polityt if partisantrial == 1
sum polityt if impartialtrial == 1
sum polityt5 if partisantrial == 1
sum polityt5 if impartialtrial == 1
sum polityimprov if partisantrial == 1
sum polityimprov if impartialtrial == 1

* Overview of all trial-classifications
tab fairtrial
tab unfairtrial
tab impartialtrial
tab partisantrial

* Time when fair trials and unfair trials were implemented
list peacesurvival if fairtrial == 1

* Characteristics of unfair trials & fair trials
list trial_domestic if fairtrial == 1
list trial_domestic if unfairtrial == 1
list trial_intl if fairtrial == 1
list trial_intl if unfairtrial == 1
** unfair trials are all domestic! (one hybrid)

* Test other pcj combined with fair trials and unfair trials
tab rep if fairtrial == 1
tab rep if impartialtrial == 1
tab rep if unfairtrial == 1
tab rep if partisantrial == 1
tab purge if fairtrial == 1
tab purge if impartialtrial == 1
tab purge if unfairtrial == 1
tab purge if partisantrial == 1
tab exile if fairtrial == 1
tab exile if impartialtrial == 1
tab exile if unfairtrial == 1
tab exile if partisantrial == 1
tab truth if fairtrial == 1
tab truth if unfairtrial == 1
tab amnesty if fairtrial == 1
tab amnesty if unfairtrial == 1

* Figures of trial ratings
gen Q7meanR = round(Q7mean)
tab Q7meanR
* Figure 2 in Article:
hist Q7meanR, freq gap(20) addlabels  discrete xlabel(0(1)10) scheme(s1mono) note("Legend: 0 = Post-conflict injustice/ 10 = Post-conflict fairness")
*graph export image1.tif, width(4900)
gen Q1meanR = round(Q1mean)
tab Q1meanR
* Figure 1 in Article:
hist Q1meanR, freq gap(20) addlabels  discrete xlabel(0(1)10) scheme(s1mono) note("Legend: 0 = Unequal treatment/ 10 = Equal treatment")
*graph export image2.tif, width(4900)

* Partisan and impartial trials over time
tab trial_year if Q7mean < 5
tab trial_year if Q7mean > 5

* Graph partisan PCTs and impartial PCTs over time
gen decade = 10 * floor(trial_year/10)
* Figure 3 in Article:
#delimit ;
graph bar (sum) unfairtrial fairtrial, over(decade) 
	   scheme(s1mono) 
	   ytitle("Frequency of PCTs") 
	   b1title("Decades of PCT implementation")
	   legend(label(1 "Partisan PCTs") label(2 "Impartial PCTs")) 
name(graph0, replace);
#delimit cr	
*graph export image3.tif, width(3900)
*save recodedtrials

* Summary statistics of variables - Table 3 in Online Appendix:
*ssc install outreg2
*outreg2 using x.doc, replace sum(log)


****************************************************************************************************************************************************

* Analysis section

* Cox survival regression command 
drop if peacesurvival == .
gen peacesur = peacesurvival if peacesurvival != 100
gen difnew = 2009-epend if peacesurvival == 100
replace peacesur = difnew if peacesur == .

* Generate failure variable
gen failure = .
replace failure = 0 if peacesurvival == 100
replace failure = 1 if peacesurvival != 100

* Model without matching: Stset peacesurvival
stset peacesur, failure(failure)
stdescribe
stvary

* Check for multicollinearity
pwcorr fairtrial unfairtrial amnesty peacekeep duration battledeath_log polityt deminter civilwar gdpcappp_log territorialconfdum victory postcold purge truth polcomp exile internal amnesty rep
reg peacesur unfairtrial amnesty peacekeep duration battledeath_log gdpcappp_log territorialconfdum victory polcomp exile internal 
vif 
reg peacesur unfairtrial amnesty peacekeep duration gdpcappp_log civilwar victory polcomp exile internal 
vif
reg peacesur unfairtrial amnesty peacekeep duration gdpcappp_log civilwar victory polcomp exile internal purge truth rep
vif 
reg peacesur unfairtrial amnesty peacekeep duration gdpcappp_log civilwar victory polcomp exile internal postcold
vif

* Logit on trials
logit trialext duration battledeath_log victory polityt civilwar gdpcappp_log territorialconfdum
logit trialext duration polcomp civilwar gdpcappp_log territorialconfdum victory postcold internal 

* Kaplan-Meier survival estimates
sts graph, by(trialext)ci scheme(sj)
gen above45 = .
replace above45 = unfairtrial if peacesur > 45
replace unfairtrial = . if peacesur > 45
sts graph, by(unfairtrial)ci scheme (sj) 
replace unfairtrial = above45 if peacesur > 45
sts graph, by(fairtrial)ci scheme (sj)

* Cox models with trials as lump-category
stcox pcj_dummy peacekeep duration battledeath_log victory polityt deminter postcold, vce(cluster location)
stcox trialext victory amnesty peacekeep duration battledeath_log polityt deminter gdpcappp_log territorialconfdum postcold, vce(cluster location)
stcox trial victory amnesty peacekeep duration polityt deminter civilwar gdpcappp_log territorialconfdum postcold, vce(cluster location)
stcox amnestyANDtrial peacekeep duration polityt deminter civilwar gdpcappp_log territorialconfdum postcold, vce(cluster location)

* Domestic and international trials
recode trial_domestic (. = 0)
recode trial_intl (. = 0)
stcox trial_domestic victory amnesty peacekeep duration battledeath_log polityt deminter civilwar gdpcap_log territorialconfdum postcold, vce(cluster location)
stcox trial_intl victory amnesty peacekeep duration battledeath_log polityt deminter civilwar gdpcap_log territorialconfdum amnestyANDtrial postcold, vce(cluster location)

* Interpretation Cox-models: 
* Importantly, to convert the coefficient to hazard ratio, calculate: e^(coefficient)=hazard ratio
* Negative sign of coeffcient with nohr-option means hazard ratio below 1: distance from 1 is decrease of hazard in percent 
* Positive sign of coefficient with nohr-option means hazard ratio above 1: distance from 1 is increase of hazard in percent (e.g. hazard ratio for age_in_years = 1.2 -> 1 year increase in age increases hazard by 20%)

* Cox models with trials differentiated
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)
gen byte used=e(sample)
stcox impartialtrial partisantrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)

* Test trial partiality as a continuous measure
*recode f1 (. = 0)
*stcox f1 peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)

* Model presentations before CEM-matching -  Table 8 in Online Appendix:
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log, vce(cluster location)
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  battledeath_log postcold, vce(cluster location)
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  civilwar, vce(cluster location)
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory battledeath_log deminter polityt, vce(cluster location)
*esttab, eform 
*stimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge, vce(cluster location)
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log internal, vce(cluster location)
*esttab, eform 
*estimates store m1
*estout m1, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using Tab_1S.rtf, eform
*eststo clear


*** IV regression ****
* Overview of all years with fair trials & with unfair trials
tab trial_year if fairtrial == 1
tab trial_year if unfairtrial == 1

* IV regressions with international diffusion definition: Defined by the number of reconciliation-seeking pcjs per year (fair trials, reparations, amnesties, truth commissions)
tab rep_year 
tab amnesty_year
tab truth_year
tab trial_year if fairtrial == 1
* based on those distributions, I created Excel-tables to calculate the sum for the four of them for every year

* Creation of the reconciliation-seeking international diffusion variable
* Important: If in one country more than one reconciliation-seeking pcj, that's not diffusion. Therefore, I controlled manually whether more reconciliation-seeking pcjs took place in the same country. 
* If yes, not counted as diffusion.
gen intdiffrec = .
replace intdiffrec = 1 if trial_year == 1953
replace intdiffrec = 1 if amnesty_year == 1955
tab pperid if amnesty_year == 1959 & fairtrial == 1
replace intdiffrec = 4 if trial_year == 1959
replace intdiffrec = 4 if amnesty_year == 1959
replace intdiffrec = 1 if amnesty_year == 1961
replace intdiffrec = 2 if amnesty_year == 1962
replace intdiffrec = 3 if amnesty_year == 1963
replace intdiffrec = 1 if amnesty_year == 1965
replace intdiffrec = 1 if amnesty_year == 1966
tab pperid if amnesty_year == 1970 & rep == 1
* amnesty + rep in the same country: therefore diffusion minus 1
replace intdiffrec = 3 if amnesty_year == 1970
replace intdiffrec = 3 if rep_year == 1970
tab pperid if amnesty_year == 1971 & rep == 1
replace intdiffrec = 4 if amnesty_year == 1971
replace intdiffrec = 4 if rep_year == 1971
replace intdiffrec = 2 if amnesty_year == 1972
tab pperid if amnesty_year == 1973 & fairtrial == 1
replace intdiffrec = 2 if trial_year == 1973
replace intdiffrec = 2 if amnesty_year == 1973
tab pperid if amnesty_year == 1974 & fairtrial == 1
replace intdiffrec = 2 if trial_year == 1974
replace intdiffrec = 2 if amnesty_year == 1974
replace intdiffrec = 2 if amnesty_year == 1975
replace intdiffrec = 2 if amnesty_year == 1976
replace intdiffrec = 2 if amnesty_year == 1977
replace intdiffrec = 2 if amnesty_year == 1978
tab pperid if rep_year == 1979 & fairtrial == 1
replace intdiffrec = 2 if rep_year == 1979
replace intdiffrec = 2 if trial_year == 1979
replace intdiffrec = 1 if amnesty_year == 1980
replace intdiffrec = 2 if amnesty_year == 1981
tab pperid if amnesty_year == 1983 & fairtrial == 1
replace intdiffrec = 2 if trial_year == 1983
replace intdiffrec = 2 if amnesty_year == 1983
replace intdiffrec = 1 if amnesty_year == 1984
replace intdiffrec = 2 if amnesty_year == 1985
replace intdiffrec = 1 if amnesty_year == 1986
replace intdiffrec = 1 if amnesty_year == 1987
replace intdiffrec = 3 if amnesty_year == 1989
tab pperid if amnesty_year == 1990 & fairtrial == 1
tab pperid if amnesty_year == 1990 & rep_year == 1990
* amnesty + fair trial + rep in the same country: therefore diffusion -2
replace intdiffrec = 2 if amnesty_year == 1990
replace intdiffrec = 2 if rep_year == 1990
replace intdiffrec = 7 if amnesty_year == 1991
tab pperid if amnesty_year == 1992 & rep_year == 1992
tab pperid if amnesty_year == 1992 & truth_year == 1992
tab pperid if rep_year == 1992 & truth_year == 1992
* amnesty + rep in the same country: diffusion -1
replace intdiffrec = 4 if amnesty_year == 1992
replace intdiffrec = 4 if truth_year == 1992
replace intdiffrec = 4 if rep_year == 1992
tab pperid if amnesty_year == 1993 & fairtrial == 1
tab pperid if rep_year == 1993 & fairtrial == 1
* amnesty + fair trial in the same country: diffusion -1
replace intdiffrec = 6 if amnesty_year == 1993
replace intdiffrec = 6 if trial_year == 1993
replace intdiffrec = 6 if rep_year == 1993
tab pperid if rep_year == 1994 & amnesty_year == 1994
tab pperid if rep_year == 1994 & truth_year == 1994
tab pperid if truth_year == 1994 & amnesty_year == 1994
* rep + truth in the same country: -1
replace intdiffrec = 11 if truth_year == 1994
replace intdiffrec = 11 if amnesty_year == 1994
replace intdiffrec = 11 if rep_year == 1994
tab pperid if rep_year == 1995 & amnesty_year == 1995
replace intdiffrec = 6 if amnesty_year == 1995
replace intdiffrec = 6 if rep_year == 1995
tab pperid if rep_year == 1996 & amnesty_year == 1996
replace intdiffrec = 6 if amnesty_year == 1996
replace intdiffrec = 6 if rep_year == 1996
tab pperid if rep_year == 1997 & amnesty_year == 1997
replace intdiffrec = 3 if amnesty_year == 1997
replace intdiffrec = 3 if rep_year == 1997
replace intdiffrec = 6 if amnesty_year == 1998
tab pperid if rep_year == 1999 & amnesty_year == 1999
* two reps + amnesties in same country: -2
replace intdiffrec = 3 if amnesty_year == 1999
replace intdiffrec = 3 if rep_year == 1999
tab pperid if rep_year == 2000 & amnesty_year == 2000
tab pperid if truth_year == 2000 & amnesty_year == 2000
tab pperid if rep_year == 2000 & truth_year == 2000
tab pperid if rep_year == 2000 & fairtrial == 1
tab pperid if amnesty_year == 2000 & fairtrial == 1
tab pperid if truth_year == 2000 & fairtrial == 1
* truth & fair trial in same county: -1
replace intdiffrec = 3 if trial_year == 2000
replace intdiffrec = 3 if amnesty_year == 2000
replace intdiffrec = 3 if truth_year == 2000
replace intdiffrec = 3 if rep_year == 2000
tab pperid if amnesty_year == 2001 & truth_year == 2001
tab pperid if truth_year == 2001 & rep_year == 2001
tab pperid if amnesty_year == 2001 & rep_year == 2001
* amnesty & rep in same country: -1
replace intdiffrec = 2 if amnesty_year == 2001
replace intdiffrec = 2 if truth_year == 2001
replace intdiffrec = 2 if rep_year == 2001
tab pperid if amnesty_year == 2002 & truth_year == 2002
replace intdiffrec = 4 if amnesty_year == 2002
replace intdiffrec = 4 if truth_year == 2002
tab pperid if amnesty_year == 2003 & truth_year == 2003
* amnesty & truth in same country: -1
replace intdiffrec = 4 if amnesty_year == 2003
replace intdiffrec = 4 if truth_year == 2003
tab pperid if amnesty_year == 2005 & truth_year == 2005
* iin same country: -1
replace intdiffrec = 1 if truth_year == 2005
replace intdiffrec = 1 if amnesty_year == 2005
tab intdiffrec
* Ascribe diffusion-values to years without pcjs
gen norec = .
replace norec = 1 if intdiffrec == . 
replace norec = 0 if intdiffrec != .
replace intdiffrec = 1 if epend == 1953 & norec == 1
replace intdiffrec = 1 if epend == 1955 & norec == 1
replace intdiffrec = 4 if epend == 1959 & norec == 1
replace intdiffrec = 1 if epend == 1961 & norec == 1
replace intdiffrec = 2 if epend == 1962 & norec == 1
replace intdiffrec = 3 if epend == 1963 & norec == 1
replace intdiffrec = 1 if epend == 1965 & norec == 1
replace intdiffrec = 1 if epend == 1966 & norec == 1
replace intdiffrec = 3 if epend == 1970 & norec == 1
replace intdiffrec = 4 if epend == 1971 & norec == 1
replace intdiffrec = 2 if epend == 1972 & norec == 1
replace intdiffrec = 2 if epend == 1973 & norec == 1
replace intdiffrec = 2 if epend == 1974 & norec == 1
replace intdiffrec = 2 if epend == 1975 & norec == 1
replace intdiffrec = 2 if epend == 1976 & norec == 1
replace intdiffrec = 2 if epend == 1977 & norec == 1
replace intdiffrec = 2 if epend == 1978 & norec == 1
replace intdiffrec = 2 if epend == 1979 & norec == 1
replace intdiffrec = 1 if epend == 1980 & norec == 1
replace intdiffrec = 2 if epend == 1981 & norec == 1
replace intdiffrec = 2 if epend == 1983 & norec == 1
replace intdiffrec = 1 if epend == 1984 & norec == 1
replace intdiffrec = 2 if epend == 1985 & norec == 1
replace intdiffrec = 1 if epend == 1986 & norec == 1
replace intdiffrec = 1 if epend == 1987 & norec == 1
replace intdiffrec = 3 if epend == 1989 & norec == 1
replace intdiffrec = 2 if epend == 1990 & norec == 1
replace intdiffrec = 7 if epend == 1991 & norec == 1
replace intdiffrec = 4 if epend == 1992 & norec == 1
replace intdiffrec = 6 if epend == 1993 & norec == 1
replace intdiffrec = 11 if epend == 1994 & norec == 1
replace intdiffrec = 6 if epend == 1995 & norec == 1
replace intdiffrec = 6 if epend == 1996 & norec == 1
replace intdiffrec = 3 if epend == 1997 & norec == 1
replace intdiffrec = 6 if epend == 1998 & norec == 1
replace intdiffrec = 3 if epend == 1999 & norec == 1
replace intdiffrec = 3 if epend == 2000 & norec == 1
replace intdiffrec = 2 if epend == 2001 & norec == 1
replace intdiffrec = 4 if epend == 2002 & norec == 1
replace intdiffrec = 4 if epend == 2003 & norec == 1
replace intdiffrec = 1 if epend == 2005 & norec == 1
tab intdiffrec
recode intdiffrec (.=0)

* International diffusion of deterrence-seeking pcjs per year (partial trials, exiles, purges)
tab trial_year if unfairtrial == 1
tab exile_year
tab purge_year

* Creation of international diffusion variable
gen intdiffdet = .
replace intdiffdet = 2 if exile_year == 1947
tab pperid if exile_year == 1948 & unfairtrial == 1948
replace intdiffdet = 2 if exile_year == 1948
replace intdiffdet = 2 if trial_year == 1948
replace intdiffdet = 5 if exile_year == 1949
tab pperid if exile_year == 1951 & purge_year == 1951
replace intdiffdet = 3 if exile_year == 1951
replace intdiffdet = 3 if purge_year == 1951
tab pperid if exile_year == 1952 & purge_year == 1952
* in same country: -1
replace intdiffdet = 1 if purge_year == 1952
replace intdiffdet = 1 if exile_year == 1952
replace intdiffdet = 1 if exile_year == 1954
replace intdiffdet = 1 if exile_year == 1955
tab pperid if purge_year == 1959 & exile_year == 1959
tab pperid if purge_year == 1959 & unfairtrial == 1
tab pperid if exile_year == 1959 & unfairtrial == 1
replace intdiffdet = 4 if exile_year == 1959
replace intdiffdet = 4 if trial_year == 1959
replace intdiffdet = 4 if purge_year == 1959
replace intdiffdet = 1 if exile_year == 1961
replace intdiffdet = 3 if exile_year == 1962
tab pperid if exile_year == 1963 & unfairtrial == 1
replace intdiffdet = 2 if exile_year == 1963
replace intdiffdet = 2 if trial_year == 1963
tab pperid if exile_year == 1964 & unfairtrial == 1
* two in same country: -2
replace intdiffdet = 3 if exile_year == 1964
replace intdiffdet = 3 if trial_year == 1964
tab pperid if exile_year == 1965 & unfairtrial == 1
* in same country: -1
replace intdiffdet = 2 if exile_year == 1965
replace intdiffdet = 2 if trial_year == 1965
replace intdiffdet = 2 if exile_year == 1966
replace intdiffdet = 2 if trial_year == 1967
replace intdiffdet = 2 if exile_year == 1970
tab pperid if exile_year == 1971 & unfairtrial == 1
tab pperid if exile_year == 1971 & purge_year == 1
tab pperid if purge_year == 1971 & unfairtrial == 1
* purge and unfairtrial in same country: -1
replace intdiffdet = 4 if exile_year == 1971
replace intdiffdet = 4 if purge_year == 1971
replace intdiffdet = 4 if trial_year == 1971
tab pperid if exile_year == 1972 & unfairtrial == 1
* same country: -1
replace intdiffdet = 2 if exile_year == 1972
replace intdiffdet = 2 if trial_year == 1972
tab pperid if purge_year == 1973 & unfairtrial == 1
replace intdiffdet = 2 if trial_year == 1973
replace intdiffdet = 2 if purge_year == 1973
tab pperid if purge_year == 1975 & exile_year == 1975
tab pperid if purge_year == 1975 & unfairtrial == 1
tab pperid if exile_year == 1975 & unfairtrial == 1
* purge & exile in same country: -1
replace intdiffdet = 3 if exile_year == 1975
replace intdiffdet = 3 if trial_year == 1975
replace intdiffdet = 3 if purge_year == 1975
replace intdiffdet = 1 if trial_year == 1976
replace intdiffdet = 1 if exile_year == 1977
replace intdiffdet = 1 if trial_year == 1978
tab pperid if purge_year == 1979 & exile_year == 1979
tab pperid if purge_year == 1979 & unfairtrial == 1
tab pperid if exile_year == 1979 & unfairtrial == 1
* purge & exile in same country: -1
replace intdiffdet = 4 if exile_year == 1979
replace intdiffdet = 4 if trial_year == 1979
replace intdiffdet = 4 if purge_year == 1979
tab pperid if exile_year == 1980 & unfairtrial == 1
* in same country: -1
replace intdiffdet = 2 if exile_year == 1980
replace intdiffdet = 2 if trial_year == 1980
replace intdiffdet = 1 if exile_year == 1981
replace intdiffdet = 2 if trial_year == 1982
tab pperid if purge_year == 1982 & unfairtrial == 1
* in same country: -1
replace intdiffdet = 2 if purge_year == 1982
tab pperid if purge_year == 1984 & unfairtrial == 1
* in same country: -1
replace intdiffdet = 2 if trial_year == 1984
replace intdiffdet = 2 if purge_year == 1984
tab pperid if exile_year == 1986 & unfairtrial == 1
* in same country: -1
replace intdiffdet = 2 if exile_year == 1986
replace intdiffdet = 2 if trial_year == 1986
replace intdiffdet = 1 if trial_year == 1987
replace intdiffdet = 1 if exile_year == 1988
tab pperid if purge_year == 1989 & exile_year == 1989
tab pperid if purge_year == 1989 & unfairtrial == 1
tab pperid if exile_year == 1989 & unfairtrial == 1
* three in same country: -3
replace intdiffdet = 3 if exile_year == 1989
replace intdiffdet = 3 if trial_year == 1989
replace intdiffdet = 3 if purge_year == 1989
replace intdiffdet = 1 if exile_year == 1990
tab pperid if purge_year == 1991 & exile_year == 1991
replace intdiffdet = 8 if exile_year == 1991
replace intdiffdet = 8 if purge_year == 1991
replace intdiffdet = 2 if exile_year == 1992
replace intdiffdet = 3 if exile_year == 1994
replace intdiffdet = 3 if trial_year == 1994
tab pperid if exile_year == 1996 & unfairtrial == 1
replace intdiffdet = 1 if purge_year == 1996
replace intdiffdet = 1 if trial_year == 1998
tab pperid if exile_year == 1999 & unfairtrial == 1
replace intdiffdet = 2 if exile_year == 1999
replace intdiffdet = 2 if trial_year == 1999
replace intdiffdet = 1 if trial_year == 2000
replace intdiffdet = 1 if exile_year == 2003
replace intdiffdet = 2 if trial_year == 2004
* Ascribe diffusion-values to years without pcjs
gen nodet = 1 if intdiffdet == .
replace nodet = 1 if intdiffdet == . 
replace nodet = 0 if intdiffdet != .
replace intdiffdet = 2 if epend == 1947 & norec == 1
replace intdiffdet = 2 if epend == 1948 & norec == 1
replace intdiffdet = 5 if epend == 1949 & norec == 1
replace intdiffdet = 3 if epend == 1951 & norec == 1
replace intdiffdet = 1 if epend == 1952 & norec == 1
replace intdiffdet = 1 if epend == 1954 & norec == 1
replace intdiffdet = 1 if epend == 1955 & norec == 1
replace intdiffdet = 4 if epend == 1959 & norec == 1
replace intdiffdet = 1 if epend == 1961 & norec == 1
replace intdiffdet = 3 if epend == 1962 & norec == 1
replace intdiffdet = 2 if epend == 1963 & norec == 1
replace intdiffdet = 3 if epend == 1964 & norec == 1
replace intdiffdet = 2 if epend == 1965 & norec == 1
replace intdiffdet = 2 if epend == 1966 & norec == 1
replace intdiffdet = 2 if epend == 1967 & norec == 1
replace intdiffdet = 2 if epend == 1970 & norec == 1
replace intdiffdet = 4 if epend == 1971 & norec == 1
replace intdiffdet = 2 if epend == 1972 & norec == 1
replace intdiffdet = 2 if epend == 1973 & norec == 1
replace intdiffdet = 3 if epend == 1975 & norec == 1
replace intdiffdet = 1 if epend == 1976 & norec == 1
replace intdiffdet = 1 if epend == 1977 & norec == 1
replace intdiffdet = 1 if epend == 1978 & norec == 1
replace intdiffdet = 4 if epend == 1979 & norec == 1
replace intdiffdet = 2 if epend == 1980 & norec == 1
replace intdiffdet = 1 if epend == 1981 & norec == 1
replace intdiffdet = 2 if epend == 1982 & norec == 1
replace intdiffdet = 2 if epend == 1984 & norec == 1
replace intdiffdet = 2 if epend == 1986 & norec == 1
replace intdiffdet = 1 if epend == 1987 & norec == 1
replace intdiffdet = 1 if epend == 1988 & norec == 1
replace intdiffdet = 3 if epend == 1989 & norec == 1
replace intdiffdet = 1 if epend == 1990 & norec == 1
replace intdiffdet = 8 if epend == 1991 & norec == 1
replace intdiffdet = 2 if epend == 1992 & norec == 1
replace intdiffdet = 3 if epend == 1994 & norec == 1
replace intdiffdet = 1 if epend == 1996 & norec == 1
replace intdiffdet = 1 if epend == 1998 & norec == 1
replace intdiffdet = 2 if epend == 1999 & norec == 1
replace intdiffdet = 1 if epend == 2000 & norec == 1
replace intdiffdet = 1 if epend == 2003 & norec == 1
replace intdiffdet = 2 if epend == 2004 & norec == 1
tab intdiffdet
recode intdiffdet (. = 0)

* IV probit models
ivprobit prelapse unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log (fairtrial = intdiffrec), first
ivprobit prelapse fairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log (unfairtrial = intdiffdet), first
estat classification
estat correlation  
lroc 
rivtest, ci

* Table 7-8 in Online Appendix:
*eststo: quietly ivprobit prelapse fairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log (unfairtrial = intdiffdet), first
*esttab 
*estimates store m1
*estout m1
*esttab using consolidationIV.rtf
*eststo clear

* Table 9-10 in Online Appendix:
*eststo: quietly ivprobit prelapse unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log (fairtrial = intdiffrec), first
*esttab 
*estimates store m1
*estout m1
*esttab using transformationIV.rtf

* Imputed missings
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log, vce(cluster location)
codebook peacekeep
codebook battledeath_log
codebook gdpcappp_log
codebook territorialconfdum
codebook victory
sts generate cumhaz = na
mi set mlong
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log, vce(cluster location)
mi register imputed gdpcappp_log
mi register regular victory peacekeep territorialconfdum duration polcomp amnesty unfairtrial peacesur failure battledeath_log fairtrial
mi xtset, clear
mi impute regress gdpcappp_log peacekeep fairtrial unfairtrial amnesty duration territorialconfdum victory polcomp battledeath_log cumhaz, add(100) rseed(10394) force
* Table 9 in Online Appendix:
mi estimate: stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log, vce(cluster location)
mi estimate: stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log, vce(cluster location) hr
mi estimate, hr
mi estimate, vartable dftable

*******************************************************************************************************************************************************************************
********************* Testing the proportional hazard assumptions *************************************************************************************************************
*******************************************************************************************************************************************************************************

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
gen gdpcap_log = log(gdpcap)
gen gdpcappp_log = log(gdppercapitappp)
gen battledeath_log = log(btldeath)

* Generate victory-dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate territorial-conflict-dummy:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Generate post-cold-war-dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Combinations of amnesties and trials
gen amnestyANDtrial = .
replace amnestyANDtrial = 0
replace amnestyANDtrial = 1 if amnesty == 1 & trial == 1
replace amnestyANDtrial = . if trial == .

* Trial scope issue
codebook trial_scope
* 1 (individuals) = 20
* 2 (elites) = 16
* 3 (specific group or subset of group) = 42
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > peacesurvival

* Generate mean-ratings of survey items
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Generate fair trial & unfair trial based on Q7
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0
tab impartial
tab partisan

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
factor Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
greigen 
loadingplot
rotate, promax
loadingplot
factor Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
loadingplot
predict f1 
* Higher values more impartial, lower values more partisan
alpha Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean Q7mean
* Cronbach alphas should be at least 0.7, here 0.81 

* Generate impactial trial & partisan trial with index
sum f1, detail
gen indexvalue = 0
replace indexvalue = f1 if f1 != .
* To find a midpoint of the index that is not distorted by the larger number of cases of post-conflict injustice, I checked its maximum value for a hyptothetical case with only tens on each question and its minimum value
* for a hypothetical case with only zeros. Those values are 2.681 and -1.744712 respectively. That means, the midpoint is 0.468144.
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.468144
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.468144
replace impartialtrial = . if f1 == .
replace impartialtrial= 0 if trialext == 0
tab impartialtrial

* Analysis section

* Cox survival regression command 
drop if peacesurvival == .
gen peacesur = peacesurvival if peacesurvival != 100
gen difnew = 2009-epend if peacesurvival == 100
replace peacesur = difnew if peacesur == .

* Generate failure variable
gen failure = .
replace failure = 0 if peacesurvival == 100
replace failure = 1 if peacesurvival != 100

* Model without matching: Stset peacesurvival
stset peacesur, failure(failure)
stdescribe
stvary

* Kaplan-Meier survival estimates
sts graph, by(trialext)ci scheme(sj)
gen above45 = .
replace above45 = unfairtrial if peacesur > 45
replace unfairtrial = . if peacesur > 45
sts graph, by(unfairtrial)ci scheme (sj) 
replace unfairtrial = above45 if peacesur > 45
sts graph, by(fairtrial)ci scheme (sj)

* Cox models with trials differentiated
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)
gen byte used=e(sample)
stcox impartialtrial partisantrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)

* Cumulative baseline hazard
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, vce(cluster location)
predict H0, basechazard
line H0 _t, c(J) sort

* Baseline survival function
predict S0, basesurv
line S0 _t, c(J) sort

* Testing the proportional-hazard assumption
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, nohr 
linktest
* Linktest verifies that the coefficient on the squared linear predictor is insignificant --> no problems with specification
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, nohr 
estat phtest, detail
* Test based on Schoenfeld residuals shows no evidence that the proportional-hazard assumption is violated
estat phtest, plot(peacekeep)
estat phtest, plot(duration)
estat phtest, plot(battledeath_log)
estat phtest, plot(victory)
estat phtest, plot(polcomp)
estat phtest, plot(gdpcappp_log)
estat phtest, plot(unfairtrial)
estat phtest, plot(fairtrial)
* Curve for all variables roughly horizontal --> confirms that proportional-hazard assumption not violated
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, nohr 
stphplot, by(unfairtrial)
stphplot, by(peacekeep)
stphplot, by(victory)
stphplot, by(polcomp)
stphplot, by(fairtrial)
* Under the proportional-hazards assumption, the plotted curves should be roughly parallel --> that is the case

* Determining the functional form of covariates with Martingale residuals
stcox, efron estimate
predict mg, mgale
lowess mg duration
lowess mg deminter
lowess mg btldeath
* Smooth plot demonstrates non-linearity at lower values of battle-deaths, therefore log-transformation necessary
lowess mg battledeath_log
* Smooth plot demonstrates non-linearity at higher values, therefore log-transformation necessary
lowess mg gdpcap_log

* Outliers and influential points
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, nohr 
predict dfb*, dfbeta
scatter dfb3 _t, yline(0) mlabel(pperid) msymbol(i)
predict ld, ldisplace
scatter ld _t, yline(0) mlabel(pperid) msymbol(i)
scatter ld _t, yline(0) mlabel(epend) msymbol(i)
* pperid 206_1996 potentially influential, test whether results hold without

* Goodness of fit using Cox-Snell residuals
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge, nohr 
predict cs, csnell
stset cs, fail(failure)
sts gen H = na
line H cs cs, sort legend(cols(1))
* By plotting the Nelson-Aalen cumulative hazard function against the Cox-Snell residuals we obtain roughly close lines --> overall fit of the model is fine


***********************************************************************************************************************************************************
****************** COARSENED EXACT MATCHING ***************************************************************************************************************
***********************************************************************************************************************************************************

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
gen gdpcap_log = log(gdpcap)
gen gdpcappp_log = log(gdppercapitappp)
gen battledeath_log = log(btldeath)

* Generate victory dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate post-cold war dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Generate territorial conflict:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Trial scope issue
codebook trial_scope
* 1 (individuals) = 20
* 2 (elites) = 16
* 3 (specific group or subset of group) = 42
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > peacesurvival


* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Combinations of amnesties and trials
gen amnestyANDtrial = .
replace amnestyANDtrial = 0
replace amnestyANDtrial = 1 if amnesty == 1 & trial == 1
replace amnestyANDtrial = . if trial == .

* Generate Mean-Ratings
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean 
factor Q2mean Q6mean Q1mean Q3mean Q4mean Q5mean 
rotate, varimax
*rotate, promax
factor Q5mean Q2mean Q6mean Q1mean Q3mean Q4mean 
predict f1

* Generate fair trial & unfair trial
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate impartial trial & partisan trial with index
sum f1, detail
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.5361
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.5361
replace impartialtrial = . if f1 == .
replace impartialtrial = 0 if trialext == 0
tab impartialtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0
tab impartial
tab partisan

* Logistic regression on trial
logit trialext battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log 

* Coarsened exact matching (CEM) preparations
tab trialext
* 58 = treated / 268 = untreated
recode trialext(. = 0)
* Exploring on which variables is imbalance - Table 2 in Article:
imb battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log, treatment(trialext)
** the most worriesome are  victory, territorial conflcit dum, battledeath_log
* CEM 
* ssc install cem
cem battledeath_log victory territorialconfdum, treatment(trialext)

* Cox survival regression command 
drop if peacesurvival == .
gen peacesur = peacesurvival if peacesurvival != 100
gen difnew = 2007-epend if peacesurvival == 100
replace peacesur = difnew if peacesur == .

* Generate failure variable
gen failure = .
replace failure = 0 if peacesurvival == 100
replace failure = 1 if peacesurvival != 100

* Stset with weights
stset peacesur [iweight=cem_weights], failure(failure)
* Cox model weighted
stcox trialext peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge
* Holistic justice: Amnesties and trials
stcox amnestyANDtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory truth rep purge
stcox fairtrial unfairtrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge
stcox partisantrial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge
stcox partisan impartial peacekeep duration battledeath_log polcomp gdpcappp_log territorialconfdum victory amnesty truth rep purge

*Model presentations - Table 3 in Article:
*eststo clear
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  battledeath_log postcold
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  civilwar
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log internal
*esttab, eform 
*estimates store m1
*estout m1, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using Tab_CEM_Results.rtf, eform
*eststo clear

* Added to Table 3 in Article:
*eststo: quietly stcox trialext amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log
*esttab, eform 
*estimates store m2
*estout m2, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using trialextonly.rtf, eform
*eststo clear

* Table 4 in Online Appendix:
*eststo: quietly stcox trialext amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
*esttab, eform
*estimates store m3
*eststo: quietly stcox partisantrial impartialtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
*esttab, eform 
*estimates store m3
*estout m3, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using partiality_ind.rtf, eform
*eststo clear

* Table 5 in Online Appendix:
*eststo: quietly stcox trialext amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
*esttab, eform
*estimates store m4
*eststo: quietly stcox partisan impartial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
*esttab, eform 
*estimates store m4
*estout m4, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using Q1predictors.rtf, eform
*eststo clear


* Survival function
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log internal, nohr basesurv(surv0)
* Cumulatuve baseline hazard
predict H0, basechazard
line H0 _t, c(J) sort
* Baseline survival function
predict S0, basesurv
line S0 _t, c(J) sort
sum battledeath_log
* mean battledeath_log = 6.578
tab victory
* mode victory = 0
tab territorialconfdum
* mode territorial conflict = 1
tab peacekeep
* mode peacekeeping = 0
sum duration
* mean duration = 4.601
sum polcomp
* mean polcomp = 4.223
sum gdpcappp_log
* mean gdp logged = 7.734
tab amnesty
* mode amnesty = 0
tab internal
* mode internal = 1
generate surv1 = surv0^exp( (0.002*6.578-0.098-0.008*4.601+0.042*4.223-0.101*7.734-0.264) )
line surv1 _t, sort ylab(0 .1 to 1) xlab(0 10 to 50)
gen surv2 = surv0^exp( (0.002*6.578-0.098-0.008*4.601+0.042*4.223-0.101*7.734-0.264-1.649) )
line surv1 surv2 _t, sort ylab(0 .1 to 1) xlab(0(1)30)
gen tt = .
replace tt = _t if _t < 35
line surv1 surv2 tt, sort ylab(0 .1 to 1) xlab(0(5)35)
* Figure 6 in Article:
line surv1 surv2 tt, c(J J) sort ylab(0 .2 to 1) xlab(0(10)35) scheme(s1mono) ytitle("Survival")

line S0 _t, c(J) sort
gen S1 = S0^0.245
label variable S0 S0
line S1 S0 _t, connect(J J) sort

* Visual representations of results with Coefplots
stcox unfairtrial fairtrial battledeath_log victory civilwar territorialconfdum peacekeep duration polityt deminter gdpcap_log amnesty postcold
coefplot, xline(0) scheme(s1mono)

* Coefplots for different model specifications
stcox fairtrial unfairtrial battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty internal truth rep 
estimates store results_1
stcox impartial partisan battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty internal truth rep 
estimates store results_2
stcox impartialtrial partisantrial battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty internal truth rep 
estimates store results_3
* Figure 5 in Article:
coefplot results_1 results_2 results_3, keep(unfairtrial partisantrial partisan fairtrial impartial impartialtrial) xline(0) scheme(s1mono) xscale(range(-3 3)) legend(off)
*line surv1 surv2 tt, sort ylab(0 .1 to 1) xlab(0(5)35)
*line surv1 surv2 tt, c(J J) sort ylab(0 .2 to 1) xlab(0(10)35) scheme(s1mono) ytitle("Survival")

*graph export image10.tif, width(3900)

* Imputed missings
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
* Missings particularly for GDP per capita
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
sts generate cumhaz = na
mi set mlong
mi xtset, clear 
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge
mi register imputed gdpcappp_log
mi register regular fairtrial unfairtrial amnesty peacekeep duration territorialconfdum victory polcomp battledeath_log truth rep purge
mi impute regress gdpcappp_log fairtrial unfairtrial amnesty peacekeep duration territorialconfdum victory polcomp battledeath_log truth rep purge cumhaz, add(100) rseed(10394) force
mi estimate: stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep purge


***********************************************************************************************************************************************************
****************** COARSENED EXACT MATCHING (with broader conceptualization of peace breakdowns) **********************************************************
***********************************************************************************************************************************************************

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
gen gdpcap_log = log(gdpcap)
gen gdpcappp_log = log(gdppercapitappp)
gen battledeath_log = log(btldeath)

* Generate victory dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Generate bargained solution dummy
gen bargainsol = .
replace bargainsol = 1 if termination == 2
replace bargainsol = 0 if termination == 1
replace bargainsol = 0 if termination == 3
replace bargainsol = 0 if termination == .

* Generate post-cold war dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Generate territorial conflict:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Trial scope issue
codebook trial_scope
* 1 (individuals) = 20
* 2 (elites) = 16
* 3 (specific group or subset of group) = 42
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > consurvival

* Combinations of amnesties and trials
gen amnestyANDtrial = .
replace amnestyANDtrial = 0
replace amnestyANDtrial = 1 if amnesty == 1 & trial == 1
replace amnestyANDtrial = . if trial == .

* Generate Mean-Ratings
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean 
factor Q2mean Q6mean Q1mean Q3mean Q4mean Q5mean 
rotate, varimax
*rotate, promax
factor Q5mean Q2mean Q6mean Q1mean Q3mean Q4mean 
predict f1

* Generate fair trial & unfair trial
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate impartial trial & partisan trial with index
sum f1, detail
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.5361
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.5361
replace impartialtrial = . if f1 == .
replace impartialtrial = 0 if trialext == 0
tab impartialtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0
tab impartial
tab partisan

* Coarsened exact matching (CEM) preparations
tab trialext
* 58 = treated / 268 = untreated
recode trialext(. = 0)
* Exploring on which variables is imbalance
* findit imb
* imb battledeath_log duration polityt victory civilwar gdpcap_log territorialconfdum, treatment(trialext)
** the most worriesome are  victory, territorial conflcit dum, civil war, batteldeath --> matching on those four makes sense theoretically and empirically
* CEM 
* ssc install cem
cem battledeath_log victory civilwar territorialconfdum, treatment(trialext)
* Exploring imbalance again
* imb battledeath_log duration polityt victory civilwar gdpcap_log territorialconfdum, treatment(trialext)

* Cox survival regression command 
drop if consurvival == .
gen peacesur = consurvival if consurvival != 100
gen difnew = 2007-epend if consurvival == 100
replace peacesur = difnew if peacesur == .

* Generate failure variable
gen failure = .
replace failure = 0 if consurvival == 100
replace failure = 1 if consurvival != 100

* Stset with weights
stset peacesur [iweight=cem_weights], failure(failure)
* Cox model weighted
stcox trialext battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log
* That's my model:
stcox unfairtrial fairtrial battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log
gen byte used=e(sample)
stcox partisantrial impartialtrial battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log
stcox partisan impartial battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log

* Model presentations - Table 6 in Online Appendix:
*eststo clear
*eststo: quietly stcox trialext battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log
*esttab, eform
*estimates store m5
*eststo: quietly stcox unfairtrial fairtrial battledeath_log victory territorialconfdum amnesty peacekeep duration polcomp gdpcappp_log
*esttab, eform 
*estimates store m5
*estout m4, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using peacewide.rtf, eform
*eststo clear


***********************************************************************************************************************************************************
****************** COARSENED EXACT MATCHING (matching on partisan PCTs) ***********************************************************************************
***********************************************************************************************************************************************************

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
gen gdpcap_log = log(gdpcap)
gen gdpcappp_log = log(gdppercapitappp)
gen battledeath_log = log(btldeath)

* Generate victory dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate bargained solution dummy
gen bargainsol = .
replace bargainsol = 1 if termination == 2
replace bargainsol = 0 if termination == 1
replace bargainsol = 0 if termination == 3
replace bargainsol = 0 if termination == .

* Generate post-cold war dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Generate territorial conflict:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Trial scope issue
codebook trial_scope
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > peacesurvival

* Generate Mean-Ratings
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean 
factor Q2mean Q6mean Q1mean Q3mean Q4mean Q5mean 
rotate, varimax
*rotate, promax
factor Q5mean Q2mean Q6mean Q1mean Q3mean Q4mean 
predict f1

* Generate fair trial & unfair trial
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate impartial trial & partisan trial with index
sum f1, detail
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.5361
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.5361
replace impartialtrial = . if f1 == .
replace impartialtrial = 0 if trialext == 0
tab impartialtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0
tab impartial
tab partisan

* Logistic regression on trial
logit partisantrial battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcap_log 

* Coarsened exact matching (CEM) preparations
tab partisantrial
* 43 = treated / 278 = untreated
recode partisantrial(. = 0)
* Exploring on which variables is imbalance
imb battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcap_log, treatment(partisantrial)
** the most worriesome are  victory & territorial conflict
* CEM 
cem victory territorialconfdum, treatment(partisantrial)

* Cox survival regression command 
drop if peacesurvival == .
gen peacesur = peacesurvival if peacesurvival != 100
gen difnew = 2007-epend if peacesurvival == 100
replace peacesur = difnew if peacesur == .

* Generate failure variable
gen failure = .
replace failure = 0 if peacesurvival == 100
replace failure = 1 if peacesurvival != 100

* Stset with weights
stset peacesur [iweight=cem_weights], failure(failure)
* Cox model weighted
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log rep truth
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp civilwar
stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log internal

*Model presentations - Table 7 in Online Appendix:
*eststo clear
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  battledeath_log postcold
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp  civilwar
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log truth rep 
*esttab, eform 
*estimates store m1
*eststo: quietly stcox fairtrial unfairtrial amnesty peacekeep duration gdpcappp_log territorialconfdum victory polcomp battledeath_log internal
*esttab, eform 
*estimates store m1
*estout m1, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using CEM_on_partisan.rtf, eform
*eststo clear

*********************************************************************************************************************************
***************  Competing risk models  *****************************************************************************************
*********************************************************************************************************************************

clear
capture log close
set more off

* Loading the dataset
cd ""
use "Replicationdata.dta", clear
log using ./log, replace

* Renaming of variables
rename Peacekeepingoperationwithin5 peacekeep
rename DemocracywithPolityIVatt polityt
rename GDPpercapitaatt gdpcap
rename Averagemilitaryexpenditure milexp

* Generate duration of conflict
gen duration = epend - epbegin +1

* Generate logged GDP per capita & logged battle deaths
gen gdpcap_log = log(gdpcap)
gen gdpcappp_log = log(gdppercapitappp)
gen battledeath_log = log(btldeath)

* Generate victory dummy
gen victory = .
replace victory = 1 if termination == 1
replace victory = 0 if termination == 2
replace victory = 0 if termination == 3
replace victory = 0 if termination == .

* Generate bargained solution dummy
gen bargainsol = .
replace bargainsol = 1 if termination == 2
replace bargainsol = 0 if termination == 1
replace bargainsol = 0 if termination == 3
replace bargainsol = 0 if termination == .

* Generate post-cold war dummy
gen postcold = 0
replace postcold = 1 if epend > 1989

* Generate type of conflict:
codebook type
gen extrasystemic = .
replace extrasystemic = 1 if type == 1
replace extrasystemic = 0 if type == 3
replace extrasystemic = 0 if type == 4
gen internal = .
replace internal = 1 if type == 3
replace internal = 0 if type == 1
replace internal = 0 if type == 4
gen internationalinternal = .
replace internationalinternal = 1 if type == 4
replace internationalinternal = 0 if type == 1
replace internationalinternal = 0 if type == 3

* Generate territorial conflict:
gen territorialconfdum =. 
replace territorialconfdum = 1 if incomp == 2
replace territorialconfdum = 0 if incomp == 1

* Gererate an interaction term for democracy variable:
gen deminter = polityt*polityt
label var deminter "Interaction Term Democracy"

* Trial scope issue
codebook trial_scope
gen trialext = trial 
replace trialext = 0 if trial_scope == 1
codebook trialext

* Trial must be before recurrence!
replace trialext = 0 if trial_year-epend > peacesurvival

* Generate Mean-Ratings
gen Q1mean = (Q1E1+Q1E2) / 2
replace Q1mean = Q1E1 if Q1E2 == .
replace Q1mean = Q1E2 if Q1E1 == .
gen Q2mean = (Q2E1+Q2E2) / 2
replace Q2mean = Q2E1 if Q2E2 == .
replace Q2mean = Q2E2 if Q2E1 == .
replace Q2mean = 2.66666 if pperid == "22_1989"
gen Q3mean = (Q3E1+Q3E2) / 2
replace Q3mean = Q3E1 if Q3E2 == .
replace Q3mean = Q3E2 if Q3E1 == .
replace Q3mean = 2.66666 if pperid == "22_1989"
gen Q4mean = (Q4E1+Q4E2) / 2
replace Q4mean = Q4E1 if Q4E2 == .
replace Q4mean = Q4E2 if Q4E1 == .
replace Q4mean = 4.33333 if pperid == "22_1989"
gen Q5mean = (Q5E1+Q5E2) / 2
replace Q5mean = Q5E1 if Q5E2 == .
replace Q5mean = Q5E2 if Q5E1 == .
replace Q5mean = 6.66666 if pperid == "22_1989"
gen Q6mean = (Q6E1+Q6E2) / 2
replace Q6mean = Q6E1 if Q6E2 == .
replace Q6mean = Q6E2 if Q6E1 == .
replace Q6mean = 4.66666 if pperid == "22_1989"
gen Q7mean = (Q7E1+Q7E2) / 2
replace Q7mean = Q7E1 if Q7E2 == .
replace Q7mean = Q7E2 if Q7E1 == .
replace Q7mean = 2.33333 if pperid == "22_1989"

* Factor Analysis
corr Q1mean Q2mean Q3mean Q4mean Q5mean Q6mean 
factor Q2mean Q6mean Q1mean Q3mean Q4mean Q5mean 
rotate, varimax
*rotate, promax
factor Q5mean Q2mean Q6mean Q1mean Q3mean Q4mean 
predict f1

* Generate fair trial & unfair trial
gen unfairtrial = trialext
replace unfairtrial = 0 if Q7mean >= 5
replace unfairtrial = . if Q7mean == .
replace unfairtrial = 0 if trialext == 0
gen fairtrial = trialext
replace fairtrial = 0 if Q7mean < 5
replace fairtrial = . if Q7mean == .
replace fairtrial = 0 if trialext == 0
tab fairtrial
tab unfairtrial

* Generate impartial trial & partisan trial with index
sum f1, detail
gen partisantrial = trialext
replace partisantrial = 0 if f1 > 0.5361
replace partisantrial = . if f1 == .
replace partisantrial = 0 if trialext == 0
tab partisantrial
gen impartialtrial = trialext
replace impartialtrial = 0 if f1 < 0.5361
replace impartialtrial = . if f1 == .
replace impartialtrial = 0 if trialext == 0
tab impartialtrial

* Generate partial & impartial trial based on Q1
gen partisan = trialext
replace partisan = 0 if Q1mean >= 5
replace partisan = . if Q1mean == .
replace partisan = 0 if trialext == 0
gen impartial = trialext
replace impartial = 0 if Q1mean < 5
replace impartial = . if Q1mean == .
replace impartial = 0 if trialext == 0

* Preparation for peacesurvival (first type of outcome variable)
drop if peacesurvival == .
gen peacesur = peacesurvival if peacesurvival != 100
gen difnew = 2009-epend if peacesurvival == 100
replace peacesur = difnew if peacesur == .

* Preparation for consurvival (second type of outcome variable)
drop if consurvival == .
gen consur = consurvival if consurvival != 100
gen diftwo = 2007-epend if consurvival == 100
replace consur = diftwo if peacesur == .

* Generate failure variable (first type of outcome variable)
gen failure = .
replace failure = 0 if peacesurvival == 100
replace failure = 1 if peacesurvival != 100

* Generate failure variable (second type of outcome variable)
gen failure2 = .
replace failure2 = 0 if consurvival == 100
replace failure2 = 1 if consurvival != 100

* Generate status-variable to indicate competing risks
gen status = failure
replace status = 2 if failure2 == 1 & failure == 0

* Run competing risk analysis as developed by Fine & Gray (1999)
stset peacesur, failure(status == 1)
stcrreg unfairtrial fairtrial battledeath_log victory civilwar territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty, compete(status ==2)
stcrreg unfairtrial fairtrial battledeath_log victory civilwar territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty truth, compete(status ==2)
stcrreg impartialtrial partisantrial battledeath_log victory civilwar territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty, compete(status ==2)
stcrreg partisan impartial battledeath_log victory civilwar territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty, compete(status ==2)
*stset peacesur, failure(status == 1) id("acdid")
*stcrreg unfairtrial fairtrial battledeath_log victory civilwar territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty, compete(status ==2)

* Reporting of results - Table 14 in Online Appendix:
eststo clear
stset peacesur, failure(status == 1)
eststo: quietly stcrreg unfairtrial fairtrial battledeath_log victory territorialconfdum peacekeep duration polcomp gdpcappp_log amnesty, compete(status ==2)
esttab, eform 
estimates store mrisk
estout mrisk, cells(b (star fmt(3)) se)  stats( N )  style(fixed)
*esttab using competing_risk.rtf, eform

* Coarsened exact matching (CEM) preparations
tab trialext
recode trialext(. = 0)
cem battledeath_log victory territorialconfdum, treatment(trialext)

* Run competing risk analysis with CEM-matched weights
stset peacesur [iweight=cem_weights], failure(status == 1)
stcrreg unfairtrial fairtrial battledeath_log victory civilwar territorialconfdum peacekeep duration polityt deminter gdpcap_log amnesty postcold, compete(status ==2)


*********************************************************************************************************************************
***************  World map *****************************************************************************************
*********************************************************************************************************************************

cd ""

*ssc install spmap
*ssc install shp2dta
*ssc install mif2dta
*shp2dta using ne_50m_admin_0_countries, database(usdb) coordinates(worldcord) genid(id)
*use usdb, clear
*describe
*list id NAME
*rename NAME location
*save usdk
*use recodedtrials, clear
*merge m:1 location using usdk
use uniquetrials, clear 
spmap Q7mean using worldcord, id(id) fcolor(red orange mint green) clmethod(custom) clbreaks(0 2 5 8 10) 
*graph export ts.tif, width(16000)

********************************************************************************************************************************************************************************
exit
